<template>
	<view class="container">
		<!-- <view class="left-bottom-sign"></view> -->
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		<!-- <view class="right-top-sign"></view> -->
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">RESET</view>
			<view class="welcome">
				密码重置
			</view>
			<view class="input-content">
				<view class="input-item">
					<input type="number" :value="username" ref="username" placeholder="请输入手机号" data-key="username" @input="inputChange" />
				</view>
				<view class="input-item">
					<input style="width: 60%;" type="text" :value="captcha" ref="captcha" placeholder="请输入短信验证码" data-key="captcha"
					 @input="inputChange" />
					<button v-if="!getting" class="btn" :disabled="sendDisable" @click="getCaptcha">获取验证码</button>
					<button v-else class="dis-btn">{{time}}秒后重新获取</button>
				</view>
				<!-- <view class="input-item">
					<input style="width: 60%;" type="code" :value="code" ref="code" placeholder="请输入图形验证码" data-key="code" @input="inputChange" />
					<image @click="getCode" :src="codeUrl" mode="aspectFit"></image>
				</view> -->
				<view class="input-item">
					<input type="text" :value="newPwd" ref="newPwd" placeholder="请输入新密码" placeholder-class="input-empty" maxlength="20"
					 password data-key="newPwd" @input="inputChange" />
				</view>
				<view class="input-item">
					<input type="text" :value="confirmNewPwd" ref="confirmNewPwd" placeholder="请确认新密码" placeholder-class="input-empty" maxlength="20"
					 password data-key="confirmNewPwd" @input="inputChange" @confirm="toLogin" />
				</view>
			</view>
			<button class="confirm-btn" @click="toLogin" :disabled="logining">重置</button>
		</view>
	</view>
</template>

<script>
	import {
		encryptPwd,
		getPublicKey,
		pwdStrategy,
		getCode,
		updatePwd,
		h5ValidPhone,
		h5CheckMsgCode
	} from '@/api/public/login.js';
	import {
		checkPassword
	} from '../../common/utils/func.js'
	import {
		baseURL,
		serv
	} from '../../common/utils/config.js'
	import JsEncrypt from "@/common/utils/jsencrypt.min.js";
	export default {
		data() {
			return {
				username: '',
				captcha: '',
				newPwd: '',
				confirmNewPwd: '',
				code: '',
				logining: false,
				getting: false,
				time: 60,
				key: '',
				strategy: {},
				pubKey: '',
				codeUrl: '',
				codeKey: '',
				uuid: '',
				sendDisable: false,
				key: '',
			};
		},
		onLoad() {
			this.getPwdStrategy()
		},
		methods: {
			getPwdStrategy() {
				pwdStrategy().then(res => {
					if (res.code === 0) {
						this.strategy = res.data;
					} else {
						this.$util.msg(res.message)
					}
				})
			},
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack() {
				uni.navigateBack({})
			},
			getCaptcha() {
				this.sendDisable = true
				if (this.username === "") {
					this.$api.msg("手机号不能为空！");
					this.sendDisable = false
					this.getting = false;
					return false;
				}
				let phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
				if (!phoneReg.test(this.username)) {
					this.$api.msg("手机号格式错误！");
					this.sendDisable = false
					this.getting = false;
					return false;
				}
				//先验证手机号码在系统中的合法性
				h5ValidPhone({
					mobile: this.username
				}).then( validRes => {
					if(validRes.code == 0){
						//获取验证码
						this.uuid = validRes.data
						this.toGetMsgCode()
					}else{
						this.sendDisable = false
						this.$util.msg(validRes.message)
					}
				})
				
			},
			toGetMsgCode(){
				
				getCode({
					uuid: this.uuid
				}).then(res => {
					if (res.code === 0) {
						this.getting = true
						this.sendDisable = false
						// this.getting = false;
						var interval = setInterval(() => {
							let times = --this.time
							this.time = times < 10 ? '0' + times : times //小于10秒补 0
						}, 1000)
						setTimeout(() => {
							this.getting = false;
							clearInterval(interval)
							this.time = 60
						}, 60000)
						this.$util.msg('发送成功')
					} else {
						this.$util.msg(res.message)
						this.getting = false;
					}
				})
			},
			toLogin() {
				this.logining = true;
				var {
					username,
					captcha,
					newPwd,
					confirmNewPwd
				} = this;
				if (username === "") {
					this.$api.msg("手机号不能为空！");
					this.logining = false;
					return false;
				}
				let phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
				if (!phoneReg.test(username)) {
					this.$api.msg("手机号格式错误！");
					this.logining = false;
					return false;
				}
				if (captcha === "") {
					this.$api.msg("短信验证码不能为空！");
					this.logining = false;
					return false;
				}
				if (newPwd === "") {
					this.$api.msg("密码不能为空！");
					this.logining = false;
					return false;
				}
				let level = checkPassword(this.strategy.length, this.newPwd)
				if (level < this.strategy.level) {
					if (level === 0) {
						this.$util.msg(`密码长度至少${this.strategy.length}位！`)
					} else {
						this.$util.msg('当前密码等级过低，请重新输入')
					}
					this.logining = false;
					return false;
				}
				if (confirmNewPwd === "" || newPwd !== confirmNewPwd) {
					this.$api.msg("两次输入的密码不一致！");
					this.logining = false;
					return false;
				}
				const p = {
					uuid: this.uuid,
					msgCode: this.captcha
				}
				h5CheckMsgCode(p).then(res => {
					if (res.code === 0) {
						//短信验证码校验成功，获取加密公钥
						getPublicKey({},username).then(res => {
							if (res.code === 0) {
								let publicKey = res.data;
								this.key = publicKey.key
								if (publicKey.publicKey) {
									newPwd = encryptPwd(publicKey,newPwd);
								}
								const params = {
									newPassWD: newPwd,
									mobile: username,
									key: this.key
								}
								updatePwd(params).then(res => {
									if (res.code === 0){
										this.$util.msg('重置成功')
										// const sendData = {
										// 	loginType: 'mobile',
										// 	userCode: username,
										// 	passwd: newPwd,
										// 	key: this.key
										// };
										//登录
										uni.reLaunch({
											url: '/pages/public/login'
										});
										// this.$store.dispatch('userLogin', sendData).then(res => {
										// 	this.logining = false;
										// 	if (res.code === 0) {
										// 		this.$util.msg('登录成功')
										// 		uni.reLaunch({
										// 			url: '/pages/index/index'
										// 		});
										// 	} else {
										// 		this.$util.msg(res.message)
										// 		this.logining = false;
										// 	}
										// })
									} else{
										this.$util.msg(res.message)
										this.logining = false;
									}
								})
							}else {
								this.$util.msg(res.message)
								this.logining = false;
							}	
						})
					} else {
						this.logining = false;
						this.$util.msg(res.message)
						return false;
					}
				})
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 50upx;
	}

	.rem {
		padding: 0 60rpx;
		/* margin-top: 10rpx; */
		display: flex;
		align-items: center;

		text {
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}
	}

	.input-item {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		padding: 0 30upx;
		background: $page-color-light;
		height: 90upx;
		border-radius: 4px;
		margin-bottom: 30upx;

		&:last-child {
			margin-bottom: 0;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm;
			color: $font-color-base;
		}

		input {
			height: 60upx;
			font-size: $font-sm+2upx;
			color: $font-color-dark;
			width: 100%;
		}

		.btn {
			height: 60rpx;
			line-height: 60rpx;
			font-size: 24rpx;
			border-radius: 18px;
			color: $base-color;
			background: #fff;
			border: 1px solid $base-color;
		}

		.dis-btn {
			height: 60rpx;
			width: 250rpx;
			line-height: 60rpx;
			font-size: 24rpx;
			border-radius: 18px;
			color: #909399;
			background: #fff;
			border: 1px solid #909399;
		}

		image {
			width: 100px;
			height: 40px;
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;

		&:after {
			border-radius: 100px;
		}
	}

	.forget-section {
		font-size: $font-sm+2upx;
		color: $font-color-spec;
		text-align: center;
		margin-top: 40upx;
	}

	.register-section {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;

		text {
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}
</style>
